<?php
class FlickrFetcher extends AbstractFetcher {
	const FLICKR_PHOTO = 0; 	//http://www.flickr.com/photos/xfragger/3879228902/
	const FLICKR_STREAM = 1;	//http://www.flickr.com/photos/xfragger/
	const FLICKR_SET = 2;		//http://www.flickr.com/photos/xfragger/sets/72157622208838730/

	/**
	 * @var phpFlickr
	 */
	private $flickr = null;
	
	/**
	 * type of flickr url
	 * @var int
	 */
	private $type;
	
	/**
	 * @see AbstractFetcher#setContentInformation()
	 */
	protected function setContentInformation(){
		if(preg_match('/http:\/\/.*flickr.com\/photos\/(.+)\/sets\/(\d+)\/?$/', $this->url, $match) !== false && count($match) == 3){
			$this->type = self::FLICKR_SET;
			$this->contentInformation = $match[2];
		}
		elseif(preg_match('/http:\/\/.*flickr.com\/photos\/([^\/]+)\/(\d+)\/?$/u', $this->url, $match) !== false && count($match) == 3){
			$this->type = self::FLICKR_PHOTO;
			$this->contentInformation = $match[2];
		}
		elseif(preg_match('/http:\/\/.*flickr.com\/photos\/([^\/]+)\/?$/', $this->url, $match) !== false && count($match) == 2){
			$this->type = self::FLICKR_STREAM;
			$this->contentInformation = $match[1];
		}
		else{
			return false;
		}
		if(!$this->initFlickr()){
			return false;
		};
		return true;
	}
	
	/**
	 * @see AbstractFetcher#setTitle()
	 */
	protected function setTitle(){
		//all work is done in initFlickr()
	}
	
	/**
	 * @see AbstractFetcher#setDescription()
	 */
	protected function setDescription(){
		//all work is done in initFlickr()
	}
	
	/**
	 * @see AbstractFetcher#setPreviewImages()
	 */
	protected function setPreviewImages(){
		//all work is done in initFlickr()
	}
	
	private function initFlickr(){
		if(!defined('FLICKR_API_KEY')){
			return false; //no FlickrAPI Key, so we use GenericFetcher for this
		}
		require_once('phpFlickr/phpFlickr.php');
		$this->flickr = new phpFlickr(FLICKR_API_KEY);
		if($this->type == self::FLICKR_SET){
			$set = $this->flickr->photosets_getInfo($this->contentInformation);
			if($set){
				$this->title = $set['title'];
				$this->description = $set['description'];
				$photos = $this->flickr->photosets_getPhotos($this->contentInformation, 'url_m', 1, MAX_PREVIEWS_PER_GET, 1, 'photos');
				foreach($photos['photoset']['photo'] as $pic){
					$this->previewImages[] = $pic['url_m'];
				}
			}
			else{
				$this->title = 'Warning';
				$this->description = 'Photoset not found';
			}
		}
		elseif($this->type == self::FLICKR_PHOTO){
			$photo = $this->flickr->photos_getInfo($this->contentInformation);
			if($photo){
				$this->title = $photo['title'];
				$this->description = $photo['description'];
				$sizes = $this->flickr->photos_getSizes($this->contentInformation);
				foreach($sizes as $size){
					if($size['label'] == 'Small'){
						$this->previewImages[] = $size['source'];
						break;
					}
				}
			}
			else{
				$this->title = 'Warning';
				$this->description = 'Photo not found';
			}
		}
		elseif($this->type == self::FLICKR_STREAM){
			$person = $this->flickr->people_findByUsername($this->contentInformation);
			if($person){
				$info = $this->flickr->people_getInfo($person['nsid']);
				$this->title = $info['username'];
				if(strtolower(substr($this->title, -1)) != 's'){
					$this->title .= 's';
				}
				$this->title .= ' Photos';
				$stream = $this->flickr->people_getPublicPhotos($person['nsid'], 1, 'url_m', MAX_PREVIEWS_PER_GET, 1, 'photos');
				foreach($stream['photos']['photo'] as $pic){
					$this->previewImages[] = $pic['url_m'];
				}
			}
		}
		return true;
	}
}